<?php
/**
 * 节点管理
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class NodeAction extends AdminAction
{
    /**
     * 节点列表
     */
    public function index()
    {
        $type  = $this->_get('type');
        $type  = $type == 'front' ? 'front' : 'admin';
        $Node  = D('Node');
        if($type == 'admin')
        {
            $nodes = $Node->getAllSystemNodeGroup();
        }
        else
        {
            $nodes = $Node->getAllSystemNodeGroup(2);
        }
        $this->assign(compact('type','nodes'));
        $this->display();
    }
    
    /**
     * 添加节点
     */
    public function add()
    {
        $Node = D('Node');
        if(IS_AJAX)
        {
            if($this->data['root_id'] == 0 && $this->data['parent_id'] == 0)
            {
                $Node->setValidate('add_root');
            }
            else
            {
                $Node->setValidate('add');
            }
            $Node->patchValidate = false;
            if($Node->create($this->data))
            {
                if($Node->add())
                {
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误');
                }
            }
            else
            {
                $this->ajaxFormReturn($Node->getError());
            }
        }
        $node_id = $this->_get('nid');
        $type    = $this->_get('type');
        if(!empty($node_id))
        {
            $where = array('id'=>$node_id);
            $this->assign('node',$Node->field(true)->where($where)->find());
        }
        elseif($type !='')
        {
            $node['type'] = $type == 'admin' ? 1 : 2;
            $this->assign('node',$node);
        }
        $this->display();
    }
    
    /**
     * 节点编辑
     */
    public function edit()
    {
        $Node = D('Node');
        if(IS_AJAX)
        {
            // 更新冲突检测
            $this->checkUpdateConflict($Node);
            if($this->data['root_id'] == 0 && $this->data['parent_id'] == 0)
            {
                $Node->setValidate('add_root');
            }
            else
            {
                $Node->setValidate('add');
            }
            if($this->data['parent_id'] != 0)
            {
                $where  = array('id' =>$this->data['parent_id']);
                $parent = $Node->field(true)->where($where)->find();
                if(!empty($parent))
                {
                    if($parent['root_flg'] == 1)
                    {
                        $this->data['root_flg'] = null;
                        $this->data['root_id'] = $parent['root_id'];
                    }
                    else
                    {
                        $this->data['root_flg'] = 1;
                        $this->data['root_id'] = $parent['id'];
                    }
                }
            }
            else
            {
                $this->data['root_id'] = 0;
            }
            $Node->patchValidate = false;
            if($Node->create($this->data))
            {
                if($Node->save())
                {
                    if($this->data['parent_id'] != 0)
                    {
                        $where  = array('parent_id' =>$this->data['id']);
                        $data   = array('root_id' => $this->data['root_id']);
                        $Node->where($where)->save($data);
                    }
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误');
                }
            }
            else
            {
                $this->ajaxFormReturn($Node->getError());
            }
        }
        $nid = $this->_get('nid');
        if($nid == '')
        {
            $show_info['message'] = '参数错误!';
            $this->error($show_info);
        }
        $where  = array('id' => $nid);
        $result = $Node->field(true)->where($where)->find();
        if(empty($result))
        {
            $show_info['message'] = '数据不存在或已被删除!';
            $this->error($show_info);
        }
        $parent_nodes = $Node->getAllSystemNodeGroup($result['type'],false);
        $this->assign(compact('result','parent_nodes'));
        $this->display();
    }
    
    /**
     * 删除
     */
    public function delete()
    {
        if(IS_AJAX)
        {
            $nid = $this->_get('nid');
            if(empty($nid))
            {
                $this->ajaxFormReturn('参数错误!');
            }
            $Node   = D('Node');
            $where  = array('parent_id' => $nid);
            $result = $Node->field('id')->where($where)->find();
            if(!empty($result))
            {
                $this->ajaxFormReturn('请先删除该节点下的子节点!');
            }
            $where  = array('id' => $nid);
            if($Node->where($where)->delete())
            {
                $this->ajaxFormReturn('删除成功!',1);
            }
            else
            {
                $this->ajaxFormReturn('系统发生错误');
            }
        }
    }
    
    /**
     * 批量更新
     */
    public function batch()
    {
        if(IS_AJAX)
        {
            $root_data = isset($_POST['root']) ? $_POST['root'] :null;
            $main_data = isset($_POST['main']) ? $_POST['main'] :null;
            $Node      = D('Node');
            // 更新ROOT节点信息
            if(!empty($root_data))
            {
                $Node->setValidate('add_root');
                foreach($root_data as $key=>$val)
                {
                    $where = array('id' => intval($key));
                    if(isset($val['status']))
                    {
                        $val['status'] = 1;
                    }
                    else
                    {
                        $val['status'] = 2;
                    }
                    if($data = $Node->create($val))
                    {
                        $Node->where($where)->save($data);
                    }
                }
            }
            // 更新节点信息
            if(!empty($main_data))
            {
                $Node->setValidate('add');
                foreach($main_data as $key=>$val)
                {
                    $where = array('id' => intval($key));
                    if(isset($val['status']))
                    {
                        $val['status'] = 1;
                    }
                    else
                    {
                        $val['status'] = 2;
                    }
                    if($data = $Node->create($val))
                    {
                        $Node->where($where)->save($data);
                    }
                }
            }
            $this->ajaxFormReturn('更新成功!',1);
        }
    }
}